package com.shundr.order;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.shundr.R;
import com.shundr.base.BaseActivity;
import com.shundr.common.util.DialogUtil;
import com.shundr.common.util.ProgressDialogHelper;
import com.shundr.common.util.ToastHelper;
import com.shundr.common.view.DialogDisplay.OnDisplayListener;
import com.shundr.frame.util.StringUtil;
import com.shundr.order.http.OrderRequest;
import com.shundr.order.model.CargoOrderInfo;

/**
 * 订单详情
 * OrderDetailsActivity
 * 
 * @author larry 2015-5-27 上午9:48:41 
 * @version 1.0.0
 *
 */
public class OrderDetailsActivity extends BaseActivity implements
		OnClickListener {
	private TextView tvOrderNum;
	private TextView tvOrderSrc;
	private TextView tvOrderDst;
	private TextView tvCargoType;
	private TextView tvCargoOwnerName;
	private TextView tvCargoContactor;
	
	private TextView tvOrderTime;
	private TextView tvTruckerName;
	private TextView tvTruckerPhone;
	private TextView tvOrderStatus;
	private TextView tvTruckerNumber;
	
	private Button btnAgree;
	private Button btnRefuse;
	private LinearLayout mLayout;
	
	private RelativeLayout mLayoutOrderDetails;
	
	private String mOrderId;
	
	private CargoOrderInfo mInfo;
	
	@SuppressLint("HandlerLeak")
	Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			try {
				switch (msg.what) {
				case OrderRequest.GET_ORDER_DETAILS_SUCCESSED:
					CargoOrderInfo info=(CargoOrderInfo)msg.obj;
					mInfo = info;
					showOrderDetails(info);
					mLayoutOrderDetails.setVisibility(View.VISIBLE);
					break;
				case OrderRequest.UPDATE_ORDER_SUCCESSED:
				    ToastHelper.toastShort(mContext, "已处理");
				    setResult(501);
				    finish();
					break;
				case OrderRequest.REQUEST_FAILED:
				    String description=(String)msg.obj;
				    ToastHelper.toastShort(mContext, description);
					break;
				default:
					break;
				}
				ProgressDialogHelper.dismiss();
			} catch (Exception e) {
				e.printStackTrace();
			}			
			super.handleMessage(msg);
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_order_details);
		tvOrderSrc = (TextView) findViewById(R.id.tv_order_detail_src_place);
		tvOrderDst = (TextView) findViewById(R.id.tv_order_detail_dest_place);
		tvOrderNum = (TextView) findViewById(R.id.tv_order_num);
		tvOrderTime = (TextView) findViewById(R.id.tv_order_time);
		tvOrderStatus = (TextView) findViewById(R.id.tv_order_status);
		tvCargoType = (TextView) findViewById(R.id.tv_cargo_type);
		tvCargoContactor = (TextView) findViewById(R.id.tv_cargo_contactor);
		tvCargoOwnerName = (TextView) findViewById(R.id.tv_cargo_owner);
		
		tvTruckerNumber = (TextView) findViewById(R.id.tv_truck_code);
		tvTruckerPhone = (TextView) findViewById(R.id.tv_truck_mobile);
		tvTruckerName = (TextView) findViewById(R.id.tv_truck_owner);

		mLayoutOrderDetails=(RelativeLayout)findViewById(R.id.sv_cargo_details);
		mLayout = (LinearLayout) findViewById(R.id.layout_bottom);
		btnAgree = (Button) findViewById(R.id.btn_confirm);
		btnRefuse = (Button) findViewById(R.id.btn_refuse);
		btnAgree.setOnClickListener(this);
		btnRefuse.setOnClickListener(this);
		mOrderId = getIntent().getStringExtra("orderId") + "";
		ProgressDialogHelper.show(mContext, "正在加载中...");
		new OrderRequest(mContext, mHandler).getOrderDetailByOrderID(mOrderId);
	}
	
	private void showOrderDetails(CargoOrderInfo cargoOrderInfo) {
         try {
			if(cargoOrderInfo!=null){
				 String orderId=cargoOrderInfo.getOrderId()+"";
				 if(orderId!=null){
					 tvOrderNum.setText(orderId);
				 }
				 String orderSrc=cargoOrderInfo.getSrcPlace();
				 if(orderSrc!=null){
					 tvOrderSrc.setText(orderSrc);
				 }
				 String orderDst=cargoOrderInfo.getDstPlace();
				 if(orderDst!=null){
					 tvOrderDst.setText(orderDst);
				 }
				 String cargotype=cargoOrderInfo.getCargoName();
				 if(cargotype!=null){
					 tvCargoType.setText(cargotype);
				 }
				 String cargoContactor=cargoOrderInfo.getCargoContactPhone();
				 if(cargotype!=null){
					 tvCargoContactor.setText(cargoContactor);
				 }
				 String cargoOwner = cargoOrderInfo.getCargoContactPerson();
				 if(cargoOwner!=null){
					 tvCargoOwnerName.setText(cargoOwner);
				 }
				 String orderTime=cargoOrderInfo.getOrderTime();
				 if(orderTime!=null){
					 tvOrderTime.setText(orderTime);
				 }
				 String contactName=cargoOrderInfo.getTruckerName();
				 if(contactName!=null){
					 tvTruckerName.setText(contactName);
				 }
				 String contactPhone=cargoOrderInfo.getTruckerPhone();
				 if(contactPhone!=null){
					 tvTruckerPhone.setText(contactPhone);
				 }
				 String truckerNum = cargoOrderInfo.getTruckerPlateNumber();
				 if(!StringUtil.isNull(truckerNum)) {
					 tvTruckerNumber.setText(truckerNum);
				 }
				 short orderCargoStatus=cargoOrderInfo.getOrderCargoStatus();
				 short orderTruckStatus=cargoOrderInfo.getOrderTruckStatus();
				 int status = getCargoStatusByType(orderCargoStatus, orderTruckStatus);     	
//				 tvOrderStatus.setText(status);
				 if(status == 1){
					 mLayout.setVisibility(View.VISIBLE);
				 }else{
					 mLayout.setVisibility(View.GONE);
				 }
				
			 }
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


	@Override
	public void onClick(View v) {
        switch (v.getId()) {
		case R.id.btn_confirm://确认			
			new OrderRequest(mContext, mHandler).updateOrderStatus(mOrderId, 1);
			break;
		case R.id.btn_refuse://拒绝
			new OrderRequest(mContext, mHandler).updateOrderStatus(mOrderId, 2);
			break;
		default:
			break;
		}
	}
	
	private int getCargoStatusByType(Short orderCargoStatus,
			Short orderTruckerSatus) {
		if (orderCargoStatus == 1 && orderTruckerSatus == 1) {
			tvOrderStatus.setText("已完成");
			return 0;//已完成
		} else if (orderCargoStatus == 1 && orderTruckerSatus == 0) {
			tvOrderStatus.setText("待处理");
			return 1;//待确认
		} else if (orderCargoStatus == 0 && orderTruckerSatus == 1) {
			tvOrderStatus.setText("待确认");
			return 2;//待处理
		} else{
			tvOrderStatus.setText("已失效");
			return 0;
		}
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getSupportMenuInflater().inflate(R.menu.menu_confirm, menu);
		menu.getItem(0).setTitle("删除");
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_confirm:
			DialogUtil.noticeAlertDialog(mContext, "删除", "是否确定删除此订单？",
					new OnDisplayListener() {

						@Override
						public void confirm() {
							ProgressDialogHelper.show(mContext, "正在删除中...");
							new OrderRequest(mContext,mHandler).updateOrderStatus(mOrderId, 9);//9是关闭(隐藏)订单
						}

						@Override
						public void cancel() {

						}
					});
			break;

		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}


	@Override
	protected void onDestroy() {
		try {
			mHandler.removeCallbacksAndMessages(null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		super.onDestroy();
	}

}
